﻿using System;
using System.Web;
using System.Web.Optimization;

namespace Mvc.RazorTools
{
	/// <summary>
	/// Abstract class implementing Mvc.RazorTools.IRazorToolsBundle.
	/// </summary>
	public abstract partial class RazorToolsBundle : IRazorToolsBundle
	{
		private string _path = string.Empty;

		internal RazorToolsBundle(string path)
		{
			this.Path = path;
		}

		/// <summary>
		/// Gets the virtual path used to reference the System.Web.Optimization. Bundle 
		/// from within a view or Web page.
		/// </summary>
		public string Path
		{
			get
			{
				return _path;
			}
			set
			{
				_path = value;
			}
		}

		/// <summary>
		/// Gets a reference to the Bundle instance.
		/// </summary>
		public Bundle Bundle
		{
			get
			{
				Bundle returnValue = BundleTable.Bundles.GetBundleFor(this.Path);

				if (returnValue == null)
				{
					returnValue = this.OnGetNewBundle();
					BundleTable.Bundles.Add(returnValue);
				}

				return returnValue;
			}
		}

		/// <summary>
		/// Specifies a set of files to be included in the System.Web.Optimization.Bundle.
		/// </summary>
		/// <param name="virtualPaths">The virtual path of the file or file pattern to be 
		/// included in the bundle.</param>
		/// <returns>The System.Web.Optimization.Bundle object itself for use in subsequent 
		/// method chaining.</returns>
		public Bundle Include(params string[] virtualPaths)
		{
			Bundle returnValue = this.Bundle;

			returnValue.Include(virtualPaths);

			return returnValue;
		}

		/// <summary>
		/// Specifies a file to be included in the System.Web.Optimization.Bundle.
		/// </summary>
		/// <param name="virtualPath">The virtual path of the file or file pattern to be 
		/// included in the bundle.</param>
		/// <param name="transforms">The System.Web.Optimization.Bundle object itself for use in subsequent 
		/// method chaining.</param>
		/// <returns>The System.Web.Optimization.Bundle object itself for use in subsequent 
		/// method chaining.</returns>
		public Bundle Include(string virtualPath, params IItemTransform[] transforms)
		{
			Bundle returnValue = this.Bundle;

			returnValue.Include(virtualPath, transforms);

			return returnValue;
		}

		/// <summary>
		/// Renders link tags for a set of paths.
		/// </summary>
		/// <returns>An HTML string containing the link tag or tags for the bundle.</returns>
		public IHtmlString Render()
		{
			return this.OnRender();
		}

		/// <summary>
		/// Creates a new bundle of the type represented by this instance.
		/// </summary>
		/// <returns>A newly created bundle.</returns>
		protected virtual Bundle OnGetNewBundle()
		{
			throw new NotImplementedException();
		}

		/// <summary>
		/// Called to render link tags for a set of paths.
		/// </summary>
		/// <returns>An HTML string containing the link tag or tags for the bundle.</returns>
		protected virtual IHtmlString OnRender()
		{
			throw new NotImplementedException();
		}
	}
}